package com.example.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pojo.dto.PageSearchDto;
import com.example.pojo.entity.User;
import com.example.properties.JwtProperties;
import com.example.result.Result;
import com.example.service.IUserService;
import com.example.utils.JwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 请求参数有大括号的参数需要用PathVariable,
 * 涉及时间的需要@DateTimeFormat(pattern = "yyyy-MM-dd")设置格式
 * 当响应数据为json格式需要使用Requestbody,使用 @RequestParam 从请求中提取查询参数、表单参数甚至是多个参数
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    //登录
    @PostMapping("/login")
    public Result handleSignIn(@RequestBody User user) {
        String token = null;
        if (user.getEmail() == null) {
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getName, user.getName());
            User user1 = userService.getOne(lambdaQueryWrapper);
            if (userService.getOne(lambdaQueryWrapper) != null) {
                Map<String, Object> claims = new HashMap<>();
                claims.put("userId", user1.getId());
               token = JwtUtil.createJWT(
                        jwtProperties.getUserSecretKey(),
                        jwtProperties.getUserTtl(),
                        claims);
            }
        } else {
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(User::getEmail, user.getEmail());
            User user2 = userService.getOne(lambdaQueryWrapper);
            if (userService.getOne(lambdaQueryWrapper) != null) {
                Map<String, Object> claims = new HashMap<>();
                claims.put("userId", user2.getId());
                token = JwtUtil.createJWT(
                        jwtProperties.getUserSecretKey(),
                        jwtProperties.getUserTtl(),
                        claims);
            }

        }
        return Result.success(token);
    }
        //注册
        @PostMapping("/signUp")
        public Result handleSignUp () {
            return Result.success();
        }

        //	修改用户信息
        @PutMapping
        public Result handleModifyProfile () {
            return Result.success();
        }

        @PostMapping("/save")
        public Result addUser (@RequestBody User user){
            userService.save(user);
            return Result.success();
        }

        //根据id删除数据
        @DeleteMapping("/{id}")
        public Result deleteById (@PathVariable Long id){
            return Result.success(userService.removeById(id));
        }

        //获取用户全部的信息
        @GetMapping
        public Result getAllData () {
            return Result.success(userService.list());
        }

        //分页查询
        @PostMapping("/page")//无条件分页查询
        public Result orderPage (@RequestBody PageSearchDto pageSearchDto){
            String message = pageSearchDto.getMessage();
            // 假设要进行无条件的分页查询，每页显示10条记录，查询第1页
            Page<User> page = new Page<>(pageSearchDto.getPageNum(), pageSearchDto.getPageSize());
            LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (message != null && StringUtils.isNoneBlank(message)) {
                lambdaQueryWrapper.like(User::getName, message);
            }
            userService.page(page, lambdaQueryWrapper); // 调用 page 方法
            return Result.success(page);
        }
    }
